<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>forwardchannelrequestpool.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_net_webchannel_forwardchannelrequestpool.js.html">forwardchannelrequestpool.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A pool of forward channel requests to enable real-time
<a name="line17"></a> * messaging from the client to server.
<a name="line18"></a> *
<a name="line19"></a> * @visibility {:internal}
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.labs.net.webChannel.ForwardChannelRequestPool&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.structs.Set&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.scope(function() {
<a name="line29"></a>// type checking only (no require)
<a name="line30"></a>var ChannelRequest = goog.labs.net.webChannel.ChannelRequest;
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * This class represents the state of all forward channel requests.
<a name="line36"></a> *
<a name="line37"></a> * @param {number=} opt_maxPoolSize The maximum pool size.
<a name="line38"></a> *
<a name="line39"></a> * @constructor
<a name="line40"></a> * @final
<a name="line41"></a> */
<a name="line42"></a>goog.labs.net.webChannel.ForwardChannelRequestPool = function(opt_maxPoolSize) {
<a name="line43"></a>  /**
<a name="line44"></a>   * The current size limit of the request pool. This limit is meant to be
<a name="line45"></a>   * read-only for an existing channel.
<a name="line46"></a>   *
<a name="line47"></a>   * When SPDY is enabled, set it to the max pool size, which will
<a name="line48"></a>   * be made configurable too.
<a name="line49"></a>   *
<a name="line50"></a>   * @type {number}
<a name="line51"></a>   * @private
<a name="line52"></a>   * @const
<a name="line53"></a>   */
<a name="line54"></a>  this.maxSize_ = ForwardChannelRequestPool.isSpdyEnabled_() ?
<a name="line55"></a>      (opt_maxPoolSize ? opt_maxPoolSize :
<a name="line56"></a>          goog.labs.net.webChannel.ForwardChannelRequestPool.MAX_POOL_SIZE_) :
<a name="line57"></a>      1;
<a name="line58"></a>
<a name="line59"></a>  /**
<a name="line60"></a>   * The container for all the pending request objects.
<a name="line61"></a>   *
<a name="line62"></a>   * @private {goog.structs.Set.&lt;ChannelRequest&gt;}
<a name="line63"></a>   */
<a name="line64"></a>  this.requestPool_ = null;
<a name="line65"></a>
<a name="line66"></a>  if (this.maxSize_ &gt; 1) {
<a name="line67"></a>    this.requestPool_ = new goog.structs.Set();
<a name="line68"></a>  }
<a name="line69"></a>
<a name="line70"></a>  /**
<a name="line71"></a>   * The single request object when the pool size is limited to one.
<a name="line72"></a>   *
<a name="line73"></a>   * @private {ChannelRequest}
<a name="line74"></a>   */
<a name="line75"></a>  this.request_ = null;
<a name="line76"></a>};
<a name="line77"></a>
<a name="line78"></a>var ForwardChannelRequestPool =
<a name="line79"></a>    goog.labs.net.webChannel.ForwardChannelRequestPool;
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * The default size limit of the request pool.
<a name="line84"></a> *
<a name="line85"></a> * @private {number}
<a name="line86"></a> */
<a name="line87"></a>ForwardChannelRequestPool.MAX_POOL_SIZE_ = 10;
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * @return {boolean} True if SPDY is enabled for the current page.
<a name="line92"></a> * @private
<a name="line93"></a> */
<a name="line94"></a>ForwardChannelRequestPool.isSpdyEnabled_ = function() {
<a name="line95"></a>  return !!(window.chrome &amp;&amp; window.chrome.loadTimes &amp;&amp;
<a name="line96"></a>      window.chrome.loadTimes() &amp;&amp; window.chrome.loadTimes().wasFetchedViaSpdy);
<a name="line97"></a>};
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * @return {boolean} True if the pool is full.
<a name="line102"></a> */
<a name="line103"></a>ForwardChannelRequestPool.prototype.isFull = function() {
<a name="line104"></a>  if (this.request_) {
<a name="line105"></a>    return true;
<a name="line106"></a>  }
<a name="line107"></a>
<a name="line108"></a>  if (this.requestPool_) {
<a name="line109"></a>    return this.requestPool_.getCount() &gt;= this.maxSize_;
<a name="line110"></a>  }
<a name="line111"></a>
<a name="line112"></a>  return false;
<a name="line113"></a>};
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/**
<a name="line117"></a> * @return {number} The current size limit.
<a name="line118"></a> */
<a name="line119"></a>ForwardChannelRequestPool.prototype.getMaxSize = function() {
<a name="line120"></a>  return this.maxSize_;
<a name="line121"></a>};
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * @return {number} The number of pending requests in the pool.
<a name="line126"></a> */
<a name="line127"></a>ForwardChannelRequestPool.prototype.getRequestCount = function() {
<a name="line128"></a>  if (this.request_) {
<a name="line129"></a>    return 1;
<a name="line130"></a>  }
<a name="line131"></a>
<a name="line132"></a>  if (this.requestPool_) {
<a name="line133"></a>    return this.requestPool_.getCount();
<a name="line134"></a>  }
<a name="line135"></a>
<a name="line136"></a>  return 0;
<a name="line137"></a>};
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * @param {ChannelRequest} req The channel request.
<a name="line142"></a> * @return {boolean} True if the request is a included inside the pool.
<a name="line143"></a> */
<a name="line144"></a>ForwardChannelRequestPool.prototype.hasRequest = function(req) {
<a name="line145"></a>  if (this.request_) {
<a name="line146"></a>    return this.request_ == req;
<a name="line147"></a>  }
<a name="line148"></a>
<a name="line149"></a>  if (this.requestPool_) {
<a name="line150"></a>    return this.requestPool_.contains(req);
<a name="line151"></a>  }
<a name="line152"></a>
<a name="line153"></a>  return false;
<a name="line154"></a>};
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>/**
<a name="line158"></a> * Adds a new request to the pool.
<a name="line159"></a> *
<a name="line160"></a> * @param {!ChannelRequest} req The new channel request.
<a name="line161"></a> */
<a name="line162"></a>ForwardChannelRequestPool.prototype.addRequest = function(req) {
<a name="line163"></a>  if (this.requestPool_) {
<a name="line164"></a>    this.requestPool_.add(req);
<a name="line165"></a>  } else {
<a name="line166"></a>    this.request_ = req;
<a name="line167"></a>  }
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * Removes the given request from the pool.
<a name="line173"></a> *
<a name="line174"></a> * @param {ChannelRequest} req The channel request.
<a name="line175"></a> * @return {boolean} Whether the request has been removed from the pool.
<a name="line176"></a> */
<a name="line177"></a>ForwardChannelRequestPool.prototype.removeRequest = function(req) {
<a name="line178"></a>  if (this.request_ &amp;&amp; this.request_ == req) {
<a name="line179"></a>    this.request_ = null;
<a name="line180"></a>    return true;
<a name="line181"></a>  }
<a name="line182"></a>
<a name="line183"></a>  if (this.requestPool_ &amp;&amp; this.requestPool_.contains(req)) {
<a name="line184"></a>    this.requestPool_.remove(req);
<a name="line185"></a>    return true;
<a name="line186"></a>  }
<a name="line187"></a>
<a name="line188"></a>  return false;
<a name="line189"></a>};
<a name="line190"></a>
<a name="line191"></a>
<a name="line192"></a>/**
<a name="line193"></a> * Clears the pool and cancel all the pending requests.
<a name="line194"></a> */
<a name="line195"></a>ForwardChannelRequestPool.prototype.cancel = function() {
<a name="line196"></a>  if (this.request_) {
<a name="line197"></a>    this.request_.cancel();
<a name="line198"></a>    this.request_ = null;
<a name="line199"></a>    return;
<a name="line200"></a>  }
<a name="line201"></a>
<a name="line202"></a>  if (this.requestPool_ &amp;&amp; !this.requestPool_.isEmpty()) {
<a name="line203"></a>    goog.array.forEach(this.requestPool_.getValues(), function(val) {
<a name="line204"></a>      val.cancel();
<a name="line205"></a>    });
<a name="line206"></a>    this.requestPool_.clear();
<a name="line207"></a>  }
<a name="line208"></a>};
<a name="line209"></a>
<a name="line210"></a>
<a name="line211"></a>/**
<a name="line212"></a> * @return {boolean} Whether there are any pending requests.
<a name="line213"></a> */
<a name="line214"></a>ForwardChannelRequestPool.prototype.hasPendingRequest = function() {
<a name="line215"></a>  return (this.request_ != null) ||
<a name="line216"></a>      (this.requestPool_ != null &amp;&amp; !this.requestPool_.isEmpty());
<a name="line217"></a>};
<a name="line218"></a>
<a name="line219"></a>
<a name="line220"></a>/**
<a name="line221"></a> * Cancels all pending requests and force the completion of channel requests.
<a name="line222"></a> *
<a name="line223"></a> * Need go through the standard onRequestComplete logic to expose the max-retry
<a name="line224"></a> * failure in the standard way.
<a name="line225"></a> *
<a name="line226"></a> * @param {!function(!ChannelRequest)} onComplete The completion callback.
<a name="line227"></a> * @return {boolean} true if any request has been forced to complete.
<a name="line228"></a> */
<a name="line229"></a>ForwardChannelRequestPool.prototype.forceComplete = function(onComplete) {
<a name="line230"></a>  if (this.request_ != null) {
<a name="line231"></a>    this.request_.cancel();
<a name="line232"></a>    onComplete(this.request_);
<a name="line233"></a>    return true;
<a name="line234"></a>  }
<a name="line235"></a>
<a name="line236"></a>  if (this.requestPool_ &amp;&amp; !this.requestPool_.isEmpty()) {
<a name="line237"></a>    goog.array.forEach(this.requestPool_.getValues(),
<a name="line238"></a>        function(val) {
<a name="line239"></a>          val.cancel();
<a name="line240"></a>          onComplete(val);
<a name="line241"></a>        });
<a name="line242"></a>    return true;
<a name="line243"></a>  }
<a name="line244"></a>
<a name="line245"></a>  return false;
<a name="line246"></a>};
<a name="line247"></a>});  // goog.scope
</pre>


</body>
</html>
